package com.yswee.mallnavi.view;

import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.yswee.framework.utils.ViewInject;
import com.yswee.framework.widget.layoutview.MLayoutView;
import com.yswee.mallnavi.R;
import com.yswee.mallnavi.activity.common.SiteActivity;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class TopBar extends MLayoutView {

	private static final int MSGWHAT_TIME = 0;
	private static final int MSGDELAY_TIME = 1000;

	private static final String PATTERN_DATE = "MM-dd";
	private static final String PATTERN_TIME = "HH:mm";
	private static final SimpleDateFormat DATEFORMAT = new SimpleDateFormat(PATTERN_DATE);
	private static final SimpleDateFormat TIMEFORMAT = new SimpleDateFormat(PATTERN_TIME);

	@ViewInject
	private View imglogo;
	@ViewInject
	private TextView tvdate;
	@ViewInject
	private TextView tvweek;
	@ViewInject
	private TextView tvtime;

	private boolean handleClick;

	public void setHandleClick(boolean value) {
		handleClick = value;
	}

	private Handler mHandler = new Handler() {
		@Override
		public void handleMessage(@NonNull Message msg) {
			super.handleMessage(msg);
			switch (msg.what) {
				case MSGWHAT_TIME:
					applyTime();
					mHandler.sendEmptyMessageDelayed(MSGWHAT_TIME, MSGDELAY_TIME);
					break;
			}
		}
	};

	public TopBar(Context context) {
		super(context);
	}
	
	public TopBar(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	@Override
	protected int getLayoutResId() {
		return R.layout.view_topbar;
	}

	@Override
	protected void onBindListener() {
		super.onBindListener();
		tvtime.setOnLongClickListener(new OnLongClickListener() {
			@Override
			public boolean onLongClick(View view) {
				if (handleClick) {
					((Activity) mContext).finish();
					System.exit(0);
					return true;
				}
				return false;
			}
		});
		imglogo.setOnLongClickListener(new OnLongClickListener() {
			@Override
			public boolean onLongClick(View view) {
				if (handleClick) {
					startActivity(SiteActivity.class);
					return true;
				}
				return false;
			}
		});
	}

	@Override
	protected void onApplyData() {
		applyTime();
	}

	@Override
	protected void onAttachedToWindow() {
		super.onAttachedToWindow();
		mHandler.sendEmptyMessageDelayed(MSGWHAT_TIME, MSGDELAY_TIME);
	}

	@Override
	protected void onDetachedFromWindow() {
		super.onDetachedFromWindow();
		mHandler.removeCallbacksAndMessages(null);
	}

	private void applyTime() {
		Date datetime = new Date();
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(datetime);
		// 获取星期几，返回的是int类型，从周日开始为1，到周六为7
		int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
		tvdate.setText(DATEFORMAT.format(datetime));
		tvtime.setText(TIMEFORMAT.format(datetime));
		tvweek.setText(getWeekDay(dayOfWeek));
	}

	private String getWeekDay(int dayOfWeek) {
		int resId = 0;
		switch (dayOfWeek) {
			case 0:
				resId = R.string.week_0;
				break;
			case 1:
				resId = R.string.week_1;
				break;
			case 2:
				resId = R.string.week_2;
				break;
			case 3:
				resId = R.string.week_3;
				break;
			case 4:
				resId = R.string.week_4;
				break;
			case 5:
				resId = R.string.week_5;
				break;
			case 6:
				resId = R.string.week_6;
				break;
		}
		if (resId != 0) {
			return mContext.getResources().getString(resId);
		} else {
			return "";
		}
	}
}
